package com.xju.dao;

import com.xju.common.utils.JdbcUtils;
import com.xju.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {
    public User login(String username, String password) {
        //操作数据库
        try {
            //获取连接
            Connection connection= JdbcUtils.getConnection();
            //sql
            String sql = "select * from user where username=? and password=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //设置参数
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            //解析结果集
            User user = null;
            while (resultSet.next()) {
                user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));

            }
            JdbcUtils.close(connection, preparedStatement, resultSet);
            return user;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
